package com.chen.controller.admin;

import com.alibaba.fastjson.JSON;
import com.chen.pojo.Court;
import com.chen.pojo.Material;
import com.chen.service.material.MaterialService;
import com.chen.util.DataGridViewResult;
import com.chen.util.SystemConstrant;
import com.chen.vo.CourtVo;
import com.chen.vo.MaterialVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/material")
public class MaterialController {

    @Resource
    private MaterialService materialService;

    /**
     * 查询物资列表
     * @param materialVo
     * @return
     */
    @RequestMapping("/list")
    public DataGridViewResult list(MaterialVo materialVo){
        //设置分页信息
        PageHelper.startPage(materialVo.getPage(),materialVo.getLimit());
        //调用分页查询方法
        List<Material> materialList = materialService.list(materialVo);
        //创建分页对象
        PageInfo<Material> pageInfo = new PageInfo<Material>(materialList);
        //返回数据
        return new DataGridViewResult(pageInfo.getTotal(),pageInfo.getList());
    }

    /**
     *  添加物资列表
     * @param material
     * @return
     */
    @RequestMapping(value = "/addMaterial",produces = {"application/json;charset=UTF-8"})
    public String addMaterial(Material material){
        Map<String, Object> map = new HashMap<String, Object>();
        if (materialService.addMaterial(material)>0){
            map.put(SystemConstrant.SUCCESS,true);
            map.put(SystemConstrant.MESSAGE,"添加成功");
        }else{
            map.put(SystemConstrant.SUCCESS,false);
            map.put(SystemConstrant.MESSAGE,"添加失败");
        }
        return JSON.toJSONString(map);
    }

    /**
     *  修改物资列表
     * @param material
     * @return
     */
    @RequestMapping(value = "/updateMaterial",produces = {"application/json;charset=UTF-8"})
    public String updateMaterial(Material material){
        Map<String, Object> map = new HashMap<String, Object>();
        if (materialService.updateMaterial(material)>0){
            map.put(SystemConstrant.SUCCESS,true);
            map.put(SystemConstrant.MESSAGE,"修改成功");
        }else{
            map.put(SystemConstrant.SUCCESS,false);
            map.put(SystemConstrant.MESSAGE,"修改失败");
        }
        return JSON.toJSONString(map);
    }

}
